package de.fmannan.addbook.views;

import org.apache.log4j.Logger;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.DrillDownAdapter;
import org.eclipse.ui.part.ViewPart;

import de.fmannan.addbook.editor.GenericEditor;
import de.fmannan.addbook.editor.OpenEditorActionDelegate;


public class ContactTableView extends ViewPart {

	public static final String ID = "de.fmannan.addbook.views.ContactTableView";
	private static Logger log = Logger.getLogger(ContactTableView.class);
	private TableViewer viewer;
	private Table table;
	private static ContactTableView self = null;
	// If the user double-clicks on a table-entry the corresponding Contact
	// should be opened in the editor
	private Action doubleClickAction;
	
	public ContactTableView () {
		self = this;
	}

	public static ContactTableView getInstance() {
		return self;
	}
	
	public void createPartControl(Composite parent) {
		
		viewer = new TableViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);
		viewer.getControl().setLayoutData(GenericEditor.getGridData(400, 300));
		viewer.setContentProvider(new TableViewContentProvider(this));
		viewer.setLabelProvider(new TableViewLabelProvider());
//		viewer.setSorter(new ContactSorter());
		viewer.setInput(getViewSite());
		
		// Create empty column-headers
		buildTable();
		makeActions();
		hookDoubleClickAction();
	}
	
	/**
	 * Builds the initial table with all present contacts.
	 */
	private void buildTable() {
		// Creating the Table that will be filled with data
		table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		TableViewerModel.createColumns(table);	
		
		// Immediately refresh the columns in order to show the data
		refresh();
	}

	/**
	 * Create and implement the ActionItems.
	 */
	private void makeActions() {
		doubleClickAction = new Action() {
			public void run() {
				
				log.debug("Double clicked on a Contact - opening editor ...");
				ISelection selection = viewer.getSelection();
				// A new editor windows should be opened in order to edit or
				// view an existing contact
				OpenEditorActionDelegate.openEditor(PlatformUI.getWorkbench()
						.getActiveWorkbenchWindow().getActivePage(), selection);
			}
		};		
	}

	private void hookDoubleClickAction() {
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				doubleClickAction.run();
			}
		});
	}
	
	private void showMessage(String message) {
		MessageDialog.openInformation(
			viewer.getControl().getShell(),
			"Contact Table",
			message);
	}
	
	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
	
	public void refresh() {
		// Empty current table and refill it
		this.viewer.getTable().removeAll();
		viewer.refresh();
	}

}
